package HomeWork;

import java.util.Random;
import java.util.Scanner;

/*
2.开发一个程序，可以生成指定位数的验证码，每位可以是数字，大小写字母。
 */
public class Test02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入需要生成验证码的位数【至少3位】：");
        int num;
        while (true) {
            num = sc.nextInt();
            if(num < 3){
                System.out.println("您输入的位数有误，请重新输入：");
            }else{
                break;
            }
        }

        StringBuilder sb = new StringBuilder("");
        int numCount = 0;
        int smallCount=0;
        int bigCount = 0;
        char[] chr = new char[num];
        Random ran = new Random();
        while (true) {
            for (int i = 0; i < num; i++) {
                int num2;
                /*这个循环用来设定验证码在数字以及小写字母范围
                '9'在码表中为57，'a'在码表中为97
                 */
                while (true) {
                    num2 = ran.nextInt(75) + 48;
                    if (num2 <= 57) {
                        numCount++;
                        break;
                    } else if (num2 >= 97) {
                        smallCount++;
                        break;
                    } else if (num2>=65 && num2 <= 90) {
                        bigCount++;
                        break;
                    }
                }
                chr[i] = (char) num2;
            }
            //验证码需要包含至少一个字母以及数字
            if(numCount != 0 && smallCount != 0 && bigCount != 0){
                break;
            }
        }
        for (int i = 0; i < chr.length; i++) {
            sb.append(chr[i]);
        }
        System.out.println(sb.toString());
    }
}
